using System;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using GameStateManagement;
namespace GameStateManagementSample
{
    public class Enemy
    {

        Texture2D Texture;

        float Speed = 5;

        Random random = new Random();
        Viewport viewport;

        // 0 - right 1 - up 2 - left 3 - down
        int direction;

        public bool active;
        public BoundingBox boundingBox;
        public Vector2 position;

        public Vector2 getCenter()
        {
            return (new Vector2((position.X + Texture.Width / 8), (position.Y + Texture.Height / 2)));
        }

        public void Initialize(ref Viewport viewport, int direction, Texture2D enemyTexture)
        {
            Texture = enemyTexture;
            this.direction = direction;
            this.viewport = viewport;
            switch (direction)
            {
                case 0:
                    position.X = viewport.Bounds.Left - Texture.Width/4;
                    position.Y = (int)(random.NextDouble() * (viewport.Bounds.Bottom - Texture.Height));
                    break;
                case 1:
                    position.X = (int)(random.NextDouble() * (viewport.Bounds.Right - Texture.Width/4));
                    position.Y = viewport.Bounds.Bottom;
                    break;
                case 2:
                    position.X = viewport.Bounds.Right;
                    position.Y = (int)(random.NextDouble() * (viewport.Bounds.Bottom - Texture.Height));
                    break;
                case 3:
                    position.X = (int)(random.NextDouble() * (viewport.Bounds.Right - Texture.Width));
                    position.Y = viewport.Bounds.Top - Texture.Height;
                    break;
                default: //should never be run!
                    position.X = viewport.Bounds.Left - Texture.Width/4;
                    position.Y = (int)random.NextDouble() * (viewport.Bounds.Bottom - Texture.Height);
                    break;
            }
            active = true;
        }

        //nothing
        public void Load_Content(ContentManager Content)
        {
        }

        public void Update()
        {
            if (active)
            {
                switch (direction)
                {
                    case 0:
                        if (position.X > viewport.Bounds.Right)
                            active = false;
                        position.X += Speed;
                        break;
                    case 1:
                        if (position.Y + Texture.Height < viewport.Bounds.Top)
                            active = false;
                        position.Y -= Speed;
                        break;
                    case 2:
                        if (position.X + Texture.Width/4 < viewport.Bounds.Left)
                            active = false;
                        position.X -= Speed;
                        break;
                    case 3:
                        position.Y += Speed;
                        if (position.Y > viewport.Bounds.Bottom)
                            active = false;
                        break;
                    default: //should never be run!
                        position.X += Speed;
                        break;
                }
                boundingBox.Min = new Vector3(position.X, position.Y, 0);
                boundingBox.Max = new Vector3(position.X + Texture.Width/4, position.Y + Texture.Height, 0);
            }
        }

        public void Draw(GameTime gameTime, SpriteBatch SpriteBatch)
        {
            if (active)
                SpriteBatch.Draw(Texture, position, new Rectangle(direction * Texture.Width / 4, 0, Texture.Width / 4, Texture.Height), Color.White);
        }

    }
}